<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <el-row>
          <el-col :span="8">
            <span><el-avatar
              size="small"
              icon="el-icon-s-data"
              class="order-histogram-icon"
            />订单</span>
          </el-col>
          <el-col :span="16">
            <el-radio-group
              v-model="cycle"
              size="small"
              class="fl-right"
              @change="onCycleSelected"
            >
              <el-radio-button label="thirtyday">
                30天
              </el-radio-button>
              <el-radio-button label="week">
                周
              </el-radio-button>
              <el-radio-button label="month">
                月
              </el-radio-button>
              <el-radio-button label="year">
                年
              </el-radio-button>
            </el-radio-group>
          </el-col>
        </el-row>
      </div>
    </template>
    <div>
      <el-row>
        <span class="title">{{ titleName || '订单量趋势' }}</span>
        <el-col :span="24">
          <div
            ref="chartRef"
            class="echart-area"
          />
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script lang="ts" setup>
import type { Cycle, IOrderCycleChartData } from '@/api/types/home'
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'
import { getOrderChartDataByCycle } from '@/api/home'
import useInnerWidth from './useInnerWidth'
const innerWidth = useInnerWidth()
const chartRef = ref<HTMLDivElement | null>(null)

const titleName = '订单量趋势'
const cycle = ref<Cycle>('thirtyday')
const data = ref<IOrderCycleChartData|null>(null)

const loadData = async (c: Cycle) => {
  data.value = await getOrderChartDataByCycle(c)
}
let chart: echarts.ECharts | null = null
const drawChart = () => {
  if (data.value) {
    const {
      yAxis,
      legend,
      xAxis,
      series
    } = data.value

    // 这里需要根据cycle去构建Echart的option
    let option: echarts.EChartsOption = {}
    /* const title = {
      text: titleName,
      left: 'left',
      textStyle: { fontSize: 14 }
    } */
    if (cycle.value === 'thirtyday') {
      option = {
        // title,
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' }
        },
        legend: {
          orient: 'horizontal',
          left: 'center',
          data: legend
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true,
              interval: 0,
              length: 1
            },
            axisLabel: {
              interval: 0,
              rotate: 45
            },
            data: xAxis
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '金额',
            min: 0,
            max: yAxis.maxprice,
            position: 'left',
            axisLabel: {
              formatter: '{value}'
            },
            axisLine: { show: false }
          },
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: yAxis.maxnum,
            position: 'right',
            axisLabel: {
              formatter: '{value}'
            },
            axisLine: { show: false }
          }
        ],
        series
      }
    }
    if (cycle.value === 'week') {
      option = {
        // title,
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' }
        },
        legend: {
          orient: 'horizontal',
          left: 'center',
          data: legend
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true,
              interval: 0,
              length: 1
            },
            axisLabel: {
              interval: 0,
              rotate: 45
            },
            data: xAxis
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '金额',
            min: 0,
            max: yAxis.maxprice,
            position: 'left',
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: yAxis.maxnum,
            position: 'right',
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series
      }
    }
    if (cycle.value === 'month') {
      option = {
        // title,
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' }
        },
        legend: {
          orient: 'horizontal',
          left: 'center',
          data: legend
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true,
              interval: 0,
              length: 1
            },
            axisLabel: {
              interval: 0,
              rotate: 45
            },
            data: xAxis
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '金额',
            min: 0,
            max: yAxis.maxprice,
            position: 'left',
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: yAxis.maxnum,
            position: 'right',
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series
      }
    }
    if (cycle.value === 'year') {
      option = {
        // title,
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'cross' }
        },
        legend: {
          orient: 'horizontal',
          left: 'center',
          data: legend
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {
              alignWithLabel: true,
              interval: 0,
              length: 1
            },
            axisLabel: {
              interval: 0,
              rotate: 45
            },
            data: xAxis
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '金额',
            min: 0,
            max: yAxis.maxprice,
            position: 'left',
            axisLabel: {
              formatter: '{value}'
            }
          },
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: yAxis.maxnum,
            position: 'right',
            axisLabel: {
              formatter: '{value}'
            }
          }
        ],
        series
      }
    }

    chart?.setOption(option, true)
  }
}

watch(innerWidth, () => {
  // 窗口大小改变，重绘数据视图
  if (data.value) {
    chart?.resize()
    drawChart()
  }
})

const onCycleSelected = async () => {
  await loadData(cycle.value)
  if (data.value) { drawChart() }
}

onMounted(async () => {
  if (chartRef.value) {
    echarts.init(chartRef.value).dispose()
    chart = echarts.init(chartRef.value)
  }
  await loadData(cycle.value)
  if (data.value) { drawChart() }
})
</script>

<style lang="scss" scoped>
.box-card {
  padding: 0px;
  margin-bottom: 10px;
  font-size: 14px;
  .title {
    font-weight: bolder;
  }
  .order-histogram-icon {
    color: rgb(24, 144, 255);
    background-color: rgb(230, 247, 255);
    margin: -10px auto;
    margin-right: 5px;
  }
}
.echart-area {
  min-width:320px;
  height:300px;
}
:deep(.el-radio-button) {
  & .el-radio-button__inner {
    border: none !important;
    box-shadow: none;
  }
  &.is-active .el-radio-button__inner {
    color: rgb(24, 144, 255);
    border: none !important;
    background-color: transparent !important;
  }

}
:deep(.el-card__body) {
  padding: 12px;
  & .title {
    font-size: 14px;
    margin-bottom: 10px;
  }
}
.fl-right {
  float: right;
}
.porid-tab {
  &.active {
    color: rgb(24, 144, 255);
  }
}
</style>
